#include "stdafx.h"
#include "LoadingIndicator.h"

using namespace MomogenkyouSystem;
using namespace MomogenkyouContent;

LoadingIndicator::LoadingIndicator()
{
	m_ScreenName = "LoadIndicator";
	m_bIsPopup	=	true;
	IsModal		=	false;
	
	std::vector<std::string>	_TextureNames;
	_TextureNames.push_back("Data/Base/Loading_Text.png");
	_TextureNames.push_back("Data/Base/Loading_Circle.png");

	m_ContentManager.LoadTextures(_TextureNames);
	m_pLoadingText		= m_ContentManager.GetTexture(_TextureNames[0]);
	m_pLoadingCircle	= m_ContentManager.GetTexture(_TextureNames[1]);

	m_fTransitionOffTime = 0.016f;
}

LoadingIndicator::~LoadingIndicator()
{
	m_ContentManager.ClearContents();
	m_pLoadingText		= NULL;
	m_pLoadingCircle	= NULL;
}


void	LoadingIndicator::Initialize()
{
	m_Viewport			= GraphicsSystem::ViewPort;
	m_Viewport.X		= 544;
	m_Viewport.Y		= 290;

	m_Viewport.Height	= 144;
	m_Viewport.Width	= 256;

	m_fSpinAngle	= 0;

	m_FadeEffect.Start(0.5f, 2.0f, 0.5f);
	m_FadeEffect.Proceed(true);

	m_Camera.SetPositionLook(float4(0, 5, -5, 1), MomogenkyouHelper::FLOAT4_ZERO);
	m_Camera.CalculatePerspective(PIHALF * 0.25f, 1.777777777777778f, 0.1f, 100.0f);
	m_Camera.CalculateView();

	m_ScreenState = Active;
}

void	LoadingIndicator::Shutdown()
{

}

void	LoadingIndicator::Update(const float & fInterval)
{
	m_fSpinAngle += fInterval * 0.5f;
	if(m_fSpinAngle > PI2)
		m_fSpinAngle -= PI2;

	m_FadeEffect.Update(fInterval);

	if(InputSystem::KeyReleased(DIK_RETURN))
		m_FadeEffect.Proceed(true);

	if(m_FadeEffect.HasEnded())
	{
		ExitScreen();
	}
}

void	LoadingIndicator::DrawGameScreen(const float & fInterval)
{
	UNREFERENCED_PARAMETER(fInterval);
	float	_Alpha	=	m_FadeEffect.Opacity();

	float4	_TextColor	(1.0f, 0.4f, 0.6f,	_Alpha);
	float4	_CircleColor(0.7f, 0.4f, 1.0f,	_Alpha);

	MATRIX	_matYRotation,	_matZRotation;

	//	Move to Corner
	GraphicsSystem::Device->SetViewport(&m_Viewport);

	//	Set Camera
	GraphicsSystem::Device->SetTransform(D3DTS_VIEW,		m_Camera.ViewMatrix());
	GraphicsSystem::Device->SetTransform(D3DTS_PROJECTION,	m_Camera.ProjectionMatrix());
	
	GraphicsSystem::Device->SetRenderState(D3DRS_ZWRITEENABLE,	false);
	//	First Circle
	{
		D3DXMatrixRotationY(&_matYRotation, m_fSpinAngle);
		GraphicsSystem::Device->SetTransform(D3DTS_WORLD, &_matYRotation);

		GraphicsSystem::Device->SetRenderState(D3DRS_SRCBLEND,	D3DBLEND_SRCALPHA);
		GraphicsSystem::Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTALPHA);
		GraphicsSystem::Device->SetTexture(0, m_pLoadingCircle->Handle);
		Quad::RenderQuad(MomogenkyouHelper::FLOAT2_ONE * 3.0f, MomogenkyouHelper::FLOAT4_ZERO, _CircleColor,	0, true);
	}
	//	Text
	{
		GraphicsSystem::Device->SetTransform(D3DTS_WORLD, &MomogenkyouHelper::MATRIX_IDENTITY);
		
		GraphicsSystem::Device->SetRenderState(D3DRS_SRCBLEND,	D3DBLEND_SRCALPHA);
		GraphicsSystem::Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		GraphicsSystem::Device->SetTexture(0, m_pLoadingText->Handle);
		Quad::RenderQuad(float2(m_pLoadingText->AspectRatio(), 1), float4(0, 0.1f, 0, 1), _TextColor,	0, true);
	}

	//	Middle Circle
	{
		D3DXMatrixRotationY(&_matYRotation, -m_fSpinAngle);
		
		GraphicsSystem::Device->SetRenderState(D3DRS_SRCBLEND,	D3DBLEND_SRCALPHA);
		GraphicsSystem::Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTALPHA);
		GraphicsSystem::Device->SetTexture(0, m_pLoadingCircle->Handle);

		D3DXMatrixRotationZ(&_matZRotation, 0.9855f);
		D3DXMatrixMultiply(&_matZRotation, &_matYRotation, &_matZRotation);
		GraphicsSystem::Device->SetTransform(D3DTS_WORLD, &_matZRotation);

		Quad::RenderQuad(MomogenkyouHelper::FLOAT2_ONE * 2.5f, MomogenkyouHelper::FLOAT4_ZERO, _CircleColor,	0, true);

		D3DXMatrixRotationZ(&_matZRotation, -0.7055f);
		D3DXMatrixMultiply(&_matZRotation, &_matYRotation, &_matZRotation);
		GraphicsSystem::Device->SetTransform(D3DTS_WORLD, &_matZRotation);
		Quad::RenderQuad(MomogenkyouHelper::FLOAT2_ONE * 2.5f, MomogenkyouHelper::FLOAT4_ZERO, _CircleColor,	0, true);
	}

	//	Set back original
	GraphicsSystem::Device->SetViewport(&GraphicsSystem::ViewPort);
	//GraphicsSystem::Device->SetRenderState(D3DRS_ZWRITEENABLE,	true);

	GraphicsSystem::Device->SetTransform(D3DTS_WORLD, &MomogenkyouHelper::MATRIX_IDENTITY);
}